home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 16 / Computer Interactive cdrom 16 - dic 98.iso / zdnetit / content / CMATHCC.ZIP / INCLUDE.ZIP / CEMATH.H < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-01  |  15.5 KB  |  489 lines

  1. /*    CEMATH.H
  2.  
  3.       Complex library for the languages C and C++.
  4.  
  5.       This header file contains all definitions for
  6.       extended-precision complex numbers (complex long double).
  7.  
  8.       Copyright (C) 1996-1998 Martin Sander
  9.       Address of the author:
  10.            Martin Sander
  11.            Sertⁿrnerstr. 11
  12.            D-37085 G÷ttingen
  13.            Germany
  14.  
  15. */
  16.  
  17.  
  18. #ifndef __CEMATH_H
  19. #define __CEMATH_H
  20.  
  21. #if !defined( _CMATH_DEFS )
  22.    #ifdef __BORLANDC__
  23.        #pragma option -a-
  24.    #else /* Visual C++, Optima++ */
  25.        #pragma pack( push,1 )
  26.    #endif /* avoid insertion of dummy bytes  */
  27.    typedef struct {float    Re, Im;} fComplex;
  28.    typedef struct {double   Re, Im;} dComplex;
  29.    #ifdef __BORLANDC__
  30.        typedef long double    extended;
  31.        typedef struct {extended Re, Im;} eComplex;
  32.        #pragma option -a.
  33.    #else /* Visual C++, Optima++ */
  34.        typedef  double extended; /* No support of 80-bit IEEE numbers.
  35.                                  So make extended equal to double    */
  36.        typedef dComplex  eComplex;
  37.        #pragma pack( pop )
  38.    #endif    /* restore default data packing  */
  39.    typedef fComplex fcomplex;
  40.    typedef dComplex dcomplex;
  41.    typedef eComplex ecomplex;  // tolerate lower case
  42.    #define _CMATH_DEFS
  43. #endif
  44. #ifdef __BORLANDC__
  45.     #include <_defs.h>
  46.     #if defined __SMALL__ || defined __MEDIUM__
  47.          #define   _VFAR  near   /* even in case of DS!=SS  */
  48.     #elif defined __FLAT__
  49.          #define  _VFAR
  50.     #else
  51.          #define   _VFAR  far
  52.     #endif
  53.     #if (__BORLANDC__ >= 0x450)
  54.          #define __cmf _RTLENTRY _EXPFUNC
  55.     #else
  56.          #define __cmf  _Cdecl _FARFUNC
  57.     #endif
  58.     #if __BORLANDC__ < 0x500
  59.         #define VBOOL int
  60.     #else
  61.         #define VBOOL bool
  62.     #endif
  63. #else  /* Visual C++, Optima++ */
  64.     #define _VFAR
  65.     #define __cmf  __cdecl
  66.     #ifndef __CDMATH_H
  67.         #include <cdmath.h>
  68.     #endif
  69. #endif
  70.  
  71. #ifdef __BORLANDC__   /* 80-bit IEEE numbers supported:
  72.                          the following 340 lines apply
  73.                          only to Borland C++  */
  74. #ifdef __cplusplus
  75.     /*  first the constructors:
  76.       since eComplex is declared as a struct instead of a class,
  77.       the constructor cannot get the name "eComplex" here.     */
  78.   #ifndef _ECPLX_DEFINED
  79.   inline eComplex __cmf ecplx( extended __ReVal )
  80.   {   eComplex Result;
  81.       Result.Re = __ReVal;
  82.       Result.Im = 0.0;
  83.       return Result;
  84.   }
  85.  
  86.       // up-conversions from single and double precision:
  87.   inline eComplex __cmf ecplx( fComplex & __zf )
  88.   {   eComplex Result;
  89.       Result.Re = __zf.Re;
  90.       Result.Im = __zf.Im;
  91.       return Result;
  92.   }
  93.  
  94.   inline eComplex __cmf ecplx( dComplex & __zd )
  95.   {   eComplex Result;
  96.       Result.Re = __zd.Re;
  97.       Result.Im = __zd.Im;
  98.       return Result;
  99.   }
  100.  
  101.   #ifdef __COMPLEX_H
  102.         // conversion from class complex
  103.      inline eComplex __cmf ecplx( complex & __zc )
  104.      {   eComplex Result;
  105.          Result.Re = real(__zc);
  106.          Result.Im = imag(__zc);
  107.          return Result;
  108.      }
  109.   #endif  // __COMPLEX_H
  110.   #define _ECPLX_DEFINED
  111.   #endif  // _ECPLX_DEFINED
  112. extern "C" {
  113. #endif   /* __cplusplus */
  114.        /* basic form of constructor for C and C++ : */
  115. eComplex __cmf ecplx( extended __ReVal, extended __ImVal);
  116.  
  117.      /* conversion from fComplex and dComplex */
  118. #if !defined __NEWCPLX_H
  119.    eComplex __cmf cftoce( fComplex __zf );
  120.    eComplex __cmf cdtoce( dComplex __zd );
  121. #endif
  122.  
  123.    /* Basic complex operations. They are defined both
  124.    for C and C++. However, for C++ you may as well use the
  125.    overloaded operators and functions defined further below. */
  126. #define         ce_real( z )  (z).Re
  127. #define         ce_imag( z )  (z).Im
  128. eComplex __cmf  ce_neg(  eComplex __z );
  129. eComplex __cmf  ce_conj( eComplex __z );
  130. extended __cmf  ce_norm( eComplex __z );
  131. extended __cmf  ce_arg(  eComplex __z );
  132. eComplex __cmf  ce_polar( extended __mag, extended __angle );
  133.  
  134. eComplex __cmf  ce_add(   eComplex __x, eComplex __y );
  135. eComplex __cmf  ce_addRe( eComplex __x, extended __yRe );
  136. eComplex __cmf  ce_sub(   eComplex __x, eComplex __y );
  137. eComplex __cmf  ce_subRe( eComplex __x, extended __yRe );  /* x - yRe */
  138. eComplex __cmf  ce_subrRe( eComplex __x, extended __yRe ); /* yRe - x */
  139. eComplex __cmf  ce_mul(   eComplex __x, eComplex __y );
  140. eComplex __cmf  ce_mulRe( eComplex __x, extended __yRe );
  141. eComplex __cmf  ce_div(   eComplex __x, eComplex __y );
  142. eComplex __cmf  ce_divRe( eComplex __x, extended __yRe );  /*  x / yRe  */
  143. eComplex __cmf  ce_divrRe( eComplex __x, extended __yRe ); /* yRe / x   */
  144.  
  145. /*  mathematical functions with error handling through _matherr: */
  146. extended __cmf  ce_abs(  eComplex __z );
  147. eComplex __cmf  ce_acos( eComplex __z );
  148. eComplex __cmf  ce_asin( eComplex __z );
  149. eComplex __cmf  ce_atan( eComplex __z );
  150. eComplex __cmf  ce_cos(  eComplex __z );
  151. eComplex __cmf  ce_cosh( eComplex __z );
  152. eComplex __cmf  ce_cubic( eComplex __z );  /* raise to the third power */
  153. eComplex __cmf  ce_exp(  eComplex __z );
  154. eComplex __cmf  ce_inv(  eComplex __z );    /*   1.0 / z   */
  155. eComplex __cmf  ce_ipow( eComplex __z, int __exponent );
  156.                                              /* raise z to integer power */
  157. eComplex __cmf  ce_ln(    eComplex __z );
  158. eComplex __cmf  ce_log(   eComplex __z ); /* same as ce_ln */
  159. eComplex __cmf  ce_log2(  eComplex __z );
  160. eComplex __cmf  ce_log10( eComplex __z );
  161. eComplex __cmf  ce_pow( eComplex __base, eComplex __exponent );
  162. eComplex __cmf  ce_powReBase( extended __base, eComplex __exponent ); /* power of real base */
  163. eComplex __cmf  ce_powReExpo( eComplex __base, extended __exponent ); /* raise z to real power */
  164.                          /* for integer exponents, use ce_ipow ! */
  165. eComplex __cmf  ce_quartic( eComplex __z );  /* raise to the fourth power */
  166. eComplex __cmf  ce_sin(  eComplex __z );
  167. eComplex __cmf  ce_sinh( eComplex __z );
  168. eComplex __cmf  ce_square( eComplex __z );
  169. eComplex __cmf  ce_sqrt( eComplex __z );
  170. eComplex __cmf  ce_tan(  eComplex __z );
  171. eComplex __cmf  ce_tanh( eComplex __z );
  172.  
  173. #ifdef __cplusplus
  174. }    //  end of the extern "C" statement
  175.  
  176. #if !defined __STD_COMPLEX && !defined __NEWCPLX_H
  177.    /* in addition to the basic operations defined above for C,
  178.       here is the same complete set of overloaded operators and
  179.       functions as offered by the class complex of C++ compilers.     */
  180.  
  181.     inline extended real( eComplex & __z )
  182.     {
  183.         return __z.Re;
  184.     }
  185.  
  186.     inline extended imag( eComplex & __z )
  187.     {
  188.         return __z.Im;
  189.     }
  190.  
  191.     inline eComplex neg( eComplex & __z1 )
  192.     {   eComplex Result;
  193.         Result.Re = -__z1.Re;
  194.         Result.Im = -__z1.Im;
  195.         return Result;
  196.     }
  197.  
  198.     inline eComplex conj( eComplex & __z)
  199.     {   eComplex Result;
  200.         Result.Re =  __z.Re;
  201.         Result.Im = -__z.Im;
  202.         return Result;
  203.     }
  204.  
  205.     inline extended norm( eComplex & __z )
  206.     {
  207.        return __z.Re * __z.Re + __z.Im * __z.Im;
  208.     }
  209.  
  210.     extended __cmf  arg(  eComplex __z );
  211.     eComplex __cmf  polar( extended Mag, extended Angle );
  212.       //  unary operators:
  213.  
  214.     inline eComplex & operator +( eComplex & __z1 )
  215.     {
  216.         return __z1;
  217.     }
  218.  
  219.     inline eComplex operator -( eComplex & __z1 )
  220.     {   eComplex Result;
  221.         Result.Re = -__z1.Re;
  222.         Result.Im = -__z1.Im;
  223.         return Result;
  224.     }
  225.  
  226.       //  binary operators:
  227.  
  228.     inline eComplex operator +( eComplex & __z1, eComplex & __z2 )
  229.     {   eComplex Result;
  230.         Result.Re = __z1.Re + __z2.Re;
  231.         Result.Im = __z1.Im + __z2.Im;
  232.         return Result;
  233.     }
  234.  
  235.     inline eComplex operator +( eComplex & __z1, extended __z2Re )
  236.     {   eComplex Result;
  237.         Result.Re = __z1.Re + __z2Re;
  238.         Result.Im = __z1.Im;
  239.         return Result;
  240.     }
  241.  
  242.     inline eComplex operator +( extended __z1Re, eComplex & __z2 )
  243.     {   eComplex Result;
  244.         Result.Re = __z1Re + __z2.Re;
  245.         Result.Im = __z2.Im;
  246.         return Result;
  247.     }
  248.  
  249.     inline eComplex operator -( eComplex & __z1, eComplex & __z2 )
  250.     {   eComplex Result;
  251.         Result.Re = __z1.Re - __z2.Re;
  252.         Result.Im = __z1.Im - __z2.Im;
  253.         return Result;
  254.     }
  255.  
  256.     inline eComplex operator -( eComplex & __z1, extended __z2Re )
  257.     {   eComplex Result;
  258.         Result.Re = __z1.Re - __z2Re;
  259.         Result.Im = __z1.Im;
  260.         return Result;
  261.     }
  262.  
  263.     inline eComplex operator -( extended __z1Re, eComplex & __z2 )
  264.     {   eComplex Result;
  265.         Result.Re = __z1Re - __z2.Re;
  266.         Result.Im = -__z2.Im;
  267.         return Result;
  268.     }
  269.  
  270.     inline eComplex operator *( eComplex & __z1, eComplex & __z2 )
  271.     {   eComplex Result;
  272.         Result.Re  = __z1.Re * __z2.Re - __z1.Im * __z2.Im;
  273.         Result.Im  = __z1.Re * __z2.Im + __z1.Im * __z2.Re;
  274.         return Result;
  275.     }
  276.  
  277.     inline eComplex operator *( eComplex & __z1, extended __z2Re )
  278.     {   eComplex Result;
  279.         Result.Re = __z1.Re * __z2Re;
  280.         Result.Im = __z1.Im * __z2Re;
  281.         return Result;
  282.     }
  283.  
  284.     inline eComplex operator *( extended __z1Re, eComplex & __z2 )
  285.     {   eComplex Result;
  286.         Result.Re  = __z1Re * __z2.Re;
  287.         Result.Im  = __z1Re * __z2.Im;
  288.         return Result;
  289.     }
  290.  
  291.     eComplex operator /( eComplex __z1, eComplex __z2 );
  292.     eComplex operator /( extended __z1Re, eComplex & __z2 );
  293.            // cannot be safely inlined for extended precision
  294.     
  295.     inline eComplex operator /( eComplex & __z1, extended __z2Re )
  296.     {   eComplex Result;
  297.         Result.Re = __z1.Re / __z2Re;
  298.         Result.Im = __z1.Im / __z2Re;
  299.         return Result;
  300.     }
  301.  
  302.     inline eComplex & operator +=( eComplex & __z1, eComplex & __z2 )
  303.     {
  304.         __z1.Re += __z2.Re;
  305.         __z1.Im += __z2.Im;
  306.         return __z1;
  307.     }
  308.  
  309.     inline eComplex & operator +=( eComplex & __z1, extended __z2Re )
  310.     {
  311.         __z1.Re += __z2Re;
  312.         return __z1;
  313.     }
  314.  
  315.     inline eComplex & operator -=( eComplex & __z1, eComplex & __z2 )
  316.     {
  317.         __z1.Re -= __z2.Re;
  318.         __z1.Im -= __z2.Im;
  319.         return __z1;
  320.     }
  321.  
  322.     inline eComplex & operator -=( eComplex & __z1, extended __z2Re )
  323.     {
  324.         __z1.Re -= __z2Re;
  325.         return __z1;
  326.     }
  327.  
  328.     inline eComplex & operator *=( eComplex & __z1, eComplex & __z2 )
  329.     {
  330.         extended tmpRe;
  331.         tmpRe   = __z1.Re * __z2.Re - __z1.Im * __z2.Im;
  332.         __z1.Im = __z1.Re * __z2.Im + __z1.Im * __z2.Re;
  333.         __z1.Re = tmpRe;
  334.         return __z1;
  335.     }
  336.  
  337.     inline eComplex & operator *=( eComplex & __z1, extended __z2Re )
  338.     {
  339.         __z1.Re *= __z2Re;
  340.         __z1.Im *= __z2Re;
  341.         return __z1;
  342.     }
  343.  
  344.     inline eComplex & operator /=( eComplex & __z1, eComplex & __z2 )
  345.     {
  346.        return __z1/__z2; // cannot be safely inlined, so use library operator /
  347.     }
  348.  
  349.     inline eComplex & operator /=( eComplex & __z1, extended __z2Re )
  350.     {
  351.         __z1.Re /= __z2Re;
  352.         __z1.Im /= __z2Re;
  353.         return __z1;
  354.     }
  355.  
  356.     inline VBOOL operator ==( eComplex & __z1, eComplex & __z2 )
  357.     {
  358.         return (__z1.Re == __z2.Re) && (__z1.Im == __z2.Im );
  359.     }
  360.  
  361.     inline VBOOL operator ==( eComplex & __z1, extended __z2Re )
  362.     {
  363.         return (__z1.Re == __z2Re) && (__z1.Im == 0.0 );
  364.     }
  365.  
  366.     inline VBOOL operator !=( eComplex & __z1, eComplex & __z2 )
  367.     {
  368.         return (__z1.Re != __z2.Re) || (__z1.Im != __z2.Im );
  369.     }
  370.  
  371.     inline VBOOL operator !=( eComplex & __z1, extended __z2Re )
  372.     {
  373.         return (__z1.Im != 0.0 ) || (__z1.Re != __z2Re);
  374.     }
  375.  
  376.   /*  C++ version of the mathematical functions defined above.
  377.       They use the same code as the C versions. In case of an error,
  378.       you get a message in which the name of the C version is
  379.       stated.
  380.       Note that these functions require complex arguments to be
  381.       passed by value, not by reference, as it is done in the member
  382.       functions of the class complex. In terms of efficiency, this
  383.       is about the same. (The math functions of the class complex
  384.       store complex results at intermediate addresses and copy them
  385.       to the desired address afterwards. This final copy is not
  386.       necessary here.)
  387.  
  388.   */
  389.  
  390.   extended __cmf  abs(  eComplex __z );
  391.   eComplex __cmf  acos( eComplex __z );
  392.   eComplex __cmf  asin( eComplex __z );
  393.   eComplex __cmf  atan( eComplex __z );
  394.   eComplex __cmf  cos(  eComplex __z );
  395.   eComplex __cmf  cosh( eComplex __z );
  396.   eComplex __cmf  cubic( eComplex __z );  /* raise to the third power */
  397.   eComplex __cmf  exp(  eComplex __z );
  398.   eComplex __cmf  inv(  eComplex __z );    /*   1.0 / z   */
  399.   eComplex __cmf  ipow( eComplex __z, int __exponent );
  400.                                             /* raise z to integer power */
  401.   eComplex __cmf  ln(   eComplex __z );
  402.   eComplex __cmf  log(  eComplex __z );  /* same as ln */
  403.   eComplex __cmf  log2( eComplex __z );
  404.   eComplex __cmf  log10( eComplex __z );
  405.   eComplex __cmf  pow( eComplex __z,  eComplex __exponent );
  406.   eComplex __cmf  pow( eComplex __z,  extended __exponent ); // identical to powReExpo
  407.   eComplex __cmf  pow( extended __base,  eComplex __exponent ); // identical to powReBase
  408.   eComplex __cmf  powReBase( extended __base, eComplex __exponent ); // power of real base
  409.   eComplex __cmf  powReExpo( eComplex __z, extended __exponent );    // raise z to real power
  410.                             // for integer exponents, use ipow !
  411.   eComplex __cmf  quartic( eComplex __z );  // raise to the fourth power
  412.   eComplex __cmf  sin(  eComplex __z );
  413.   eComplex __cmf  sinh( eComplex __z );
  414.   eComplex __cmf  square( eComplex __z );
  415.   eComplex __cmf  sqrt( eComplex __z );
  416.   eComplex __cmf  tan(  eComplex __z );
  417.   eComplex __cmf  tanh( eComplex __z );
  418. #endif  /* __STD_COMPLEX , __NEWCPLX_H  */
  419. #endif  /* __cplusplus */
  420.  
  421. #else /* no 80-bit IEEE number support:
  422.         The following 50 lines apply only to
  423.         Visual C++ and Optima++:          */
  424.  
  425. #define ecplx     dcplx
  426. #define cftoce    cftocd
  427. #define cdtoce(z) (z)
  428. #define ce_real( z )  (z).Re
  429. #define ce_imag( z )  (z).Im
  430. #define ce_neg      cd_neg
  431. #define ce_conj     cd_conj
  432. #define ce_norm     cd_norm
  433. #define ce_arg      cd_arg
  434. #define ce_polar    cd_polar
  435. #define ce_add      cd_add
  436. #define ce_addRe    cd_addRe
  437. #define ce_sub      cd_sub
  438. #define ce_subRe    cd_subRe
  439. #define ce_subrRe   cd_subrRe
  440. #define ce_mul      cd_mul
  441. #define ce_mulRe    cd_mulRe
  442. #define ce_div      cd_div
  443. #define ce_divRe    cd_divRe
  444. #define ce_divrRe   cd_divrRe
  445.  
  446. #define ce_abs      cd_abs
  447. #define ce_acos     cd_acos
  448. #define ce_asin     cd_asin
  449. #define ce_atan     cd_atan
  450. #define ce_cos      cd_cos
  451. #define ce_cosh     cd_cosh
  452. #define ce_cubic    cd_cubic
  453. #define ce_exp      cd_exp
  454. #define ce_inv      cd_inv
  455. #define ce_ipow     cd_ipow
  456. #define ce_log      cd_log
  457. #define ce_log2     cd_log2
  458. #define ce_log10    cd_log10
  459. #define ce_pow      cd_pow
  460. #define ce_powReBase    cd_powReBase
  461. #define ce_powReExpo    cd_powReExpo
  462. #define ce_quartic  cd_quartic
  463. #define ce_sin      cd_sin
  464. #define ce_sinh     cd_sinh
  465. #define ce_square   cd_square
  466. #define ce_sqrt     cd_sqrt
  467. #define ce_tan      cd_tan
  468. #define ce_tanh     cd_tanh
  469.  
  470. #endif  /* Borland C++, Visual C++, or Optima++ */
  471.  
  472.  
  473. /***  user-accessible error handling functions, borrowed from VectorLib  ****/
  474.  
  475. #ifdef __cplusplus
  476. extern "C" {
  477. #endif
  478.  
  479. void  __cmf  V_noteError( char _VFAR *fname, unsigned why );
  480. void  __cmf  V_printErrorMsg( char _VFAR *ErrMsg );
  481. void  __cmf  V_setErrorEventFile( char _VFAR *filename,  unsigned ScreenAndFile );
  482. void  __cmf  V_closeErrorEventFile( void );
  483.  
  484. #ifdef __cplusplus
  485. }   // end of extern "C"
  486. #endif
  487.  
  488. #endif /*  __CEMATH_H  */
  489.